package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.ResponseResult;
import com.example.demo.entity.Supermarket;
import com.example.demo.mapper.SupermarketMapper;
import com.example.demo.service.SupermarketService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupermarketServiceImpl extends ServiceImpl<SupermarketMapper, Supermarket> implements SupermarketService {
    @Override
    public ResponseResult getMessage() {
        List<Supermarket> list = list(null);
        return ResponseResult.ok(list);
    }

    /**
     * 实现更新超市实体类信息
     * @param supermarket
     * @return
     */
    @Override
    public ResponseResult updateSupermarket(Supermarket supermarket) {
        // 更具超市名称来更新
        String name = supermarket.getName();
        boolean isSuccess = update(supermarket, new QueryWrapper<Supermarket>().eq("Supermarket_name", name));

        if(!isSuccess){
            return ResponseResult.fail();
        }
        return ResponseResult.ok();
    }

    /**
     * 新增超市实体类
     * @param supermarket
     * @return
     */
    @Override
    public ResponseResult saveSupermarket(Supermarket supermarket) {
        boolean isSuccess = save(supermarket);

        if(!isSuccess){
            return ResponseResult.fail();
        }
        return ResponseResult.ok();
    }
}
